package com.mall.bargain.task;

import com.mall.bargain.mapper.BargainMapper;
import com.mall.constant.strings.IntStant;
import com.mall.entity.bargain.BgBargain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: W
 * @date: 2021/07/21/15:06
 * Other: 定时任务类  定时刷新砍价活动状态
 */
@Component
public class BargainTask {

    @Autowired
    private BargainMapper bargainMapper;

    @Scheduled(cron = "* * 0 * * * ")
    public void updateBargainStatus() {
        List<BgBargain> list = bargainMapper.findList();
        List<BgBargain> notList = list.stream().filter(bgBargain -> {
            return bgBargain.getStartingTime().getTime() >= System.currentTimeMillis();
        }).collect(Collectors.toList());
        notList.stream().forEach(bargain ->{
            bargainMapper.updateStatus(bargain.getId(), IntStant.NOT_YET_OPEN);
        });
        List<BgBargain> overList = list.stream().filter(bgBargain -> {
            return bgBargain.getEndTime().getTime() <= System.currentTimeMillis();
        }).collect(Collectors.toList());
        overList.stream().forEach(bgBargain -> {
            bargainMapper.updateStatus(bgBargain.getId(),IntStant.ACTIVITY_END);
        });
    }
}
